<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>公告管理页</title>
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/static/css/public.css" media="all" />
</head>
<!-- 公告管理页面 -->
<body class="childrenBody">
<form class="layui-form">
    <blockquote class="layui-elem-quote">
        <div class="layui-inline">
            <a class="layui-btn layui-btn-normal addInformation_btn">添加资讯</a>
        </div>

    </blockquote>
</form>
<table id="information" lay-filter="information"></table>
<script type="text/html" id="InformationToolBar">
    <a class="layui-btn layui-btn-xs layui-bg-blue" lay-event="update">修改</a>
    <a class="layui-btn layui-btn-xs layui-bg-red" lay-event="delete">下架</a>
</script>
</body>
<script type="text/javascript" src="../../layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['table','form','jquery','laypage'],function () {
        var form = layui.form,
            table = layui.table,
            page = layui.laypage,
            $ = layui.jquery;

        //公告数据
        var informations = table.render({
            elem:"#information",
            url : '/information/list',
            cellMinWidth : 95,
            limit : 15,
            limits : [5,10,15,20,25],
            page:true,
            id:'informations',
            response: {
                statusName: 'code' //规定数据状态的字段名称，默认：code
                , statusCode: 200 //规定成功的状态码，默认：0
                , msgName: 'msg' //规定状态信息的字段名称，默认：msg
                , countName: 'count' //规定数据总数的字段名称，默认：count
                , dataName: 'data' //规定数据列表的字段名称，默认：data
            },
            cols : [[
                {type: "checkbox", fixed:"left", width:50},
                {field: 'id',hide:true, title: 'ID', width:60, align:"center"},
                {field: 'infTitle', title: '资讯标题', width:350},
                {field: 'infContext', title: '资讯内容', align:'center'},
                {field: 'infTime', title: '发布时间', align:'center', minWidth:110,width:150},
                {field:'操作',templet:'#InformationToolBar',width:200}
            ]]
        });


        //下架资讯
        table.on('tool(information)',function (row) {
            var data = row.data;
            var layEvent = row.event;
            console.log(data.id);
            if (layEvent === 'delete'){
                layer.confirm('确定要下架该条资讯吗？', {icon: 3, title: '提示信息'}, function (index) {
                    $.ajax({
                        url: '/information/deleteInformation',
                        data:{id:data.id},
                        type: 'POST',
                        success: function (res) {
                            if (res.code === 200) {
                                informations.reload();
                                layer.msg("下架成功!");
                            } else {
                                layer.msg("下架失败,请稍后再重试!");
                            }
                        }
                    });
                });
            }else{
                update(data);
            }
        });




        //修改资讯
       function update(obj){
           var index = layui.layer.open({
               title : "修改资讯",
               type : 2,
               content : "./updateInformation.html",
               success : function(layero, index){
                   var body = layui.layer.getChildFrame('body', index);
                   if(obj != null){
                       body.find(".id").val(obj.id);
                       body.find(".infTitle").val(obj.infTitle);
                       body.find("#infContext").val(obj.infContext);
                       form.render();
                   }
                   setTimeout(function(){
                       layui.layer.tips('点击此处返回资讯列表', '.layui-layer-setwin .layui-layer-close', {
                           tips: 3
                       });
                   },1000)
               }
           })
           layui.layer.full(index);
           //改变窗口大小时，重置弹窗的宽高，防止超出可视区域
           $(window).on("resize",function(){
               layui.layer.full(index);
           })
       };



        /*添加资讯*/
        $('.addInformation_btn').click(function () {
            addInformation();
        });

        //添加资讯
        function addInformation(){
            var index = layui.layer.open({
                title : "添加公告",
                type : 2,
                content : "./add_information.html",
                success : function(layero, index){
                    setTimeout(function(){
                        layui.layer.tips('点击此处返回资讯列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },1000)
                }
            });
            layui.layer.full(index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域
            $(window).on("resize",function(){
                layui.layer.full(index);
            })
        }
    });

</script>
</html>

















